/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.procurement.service;

import java.util.List;

import com.javaeye.common.util.DateUtils;
import com.javaeye.procurement.dao.StatementRecordDAO;
import com.javaeye.procurement.dao.SupplierOrderDAO;
import com.javaeye.procurement.dto.StatementRecord;
import com.javaeye.procurement.dto.SupplierOrder;
import com.javaeye.store.dao.MaterialsBatchDetailDAO;
import com.javaeye.store.dao.MaterialsChangeDetailDAO;
import com.javaeye.store.dao.MaterialsSKUDetailDAO;
import com.javaeye.store.dto.MaterialsBatchDetail;
import com.javaeye.store.dto.MaterialsChangeDetail;
import com.javaeye.store.dto.MaterialsSKUDetail;

public class BackSupplierOutStoreService implements IBackSupplierOutStoreService {
	private MaterialsChangeDetailDAO materialsChangeDetailDao;
	
	private MaterialsSKUDetailDAO materialsSKUDetailDao;
	
	private MaterialsBatchDetailDAO materialsBatchDetailDao;
	
	private SupplierOrderDAO orderDao;
	
	private StatementRecordDAO statementRecordDao;
	
	public void setStatementRecordDao(StatementRecordDAO statementRecordDao) {
		this.statementRecordDao = statementRecordDao;
	}
	
	public void setMaterialsChangeDetailDao(
			MaterialsChangeDetailDAO materialsChangeDetailDao) {
		this.materialsChangeDetailDao = materialsChangeDetailDao;
	}

	public void setMaterialsSKUDetailDao(MaterialsSKUDetailDAO materialsSKUDetailDao) {
		this.materialsSKUDetailDao = materialsSKUDetailDao;
	}

	public void setMaterialsBatchDetailDao(
			MaterialsBatchDetailDAO materialsBatchDetailDao) {
		this.materialsBatchDetailDao = materialsBatchDetailDao;
	}
	
	public void setOrderDao(SupplierOrderDAO orderDao) {
		this.orderDao = orderDao;
	}
	
	public void saveOutStoreInfo(SupplierOrder order, List<MaterialsBatchDetail> batchDetails) {
		//更新状态
		order.setStatus(SupplierOrder.ORDER_STATES_ALL_RECEIVE);
		order.setUpdateDate(DateUtils.now());
		orderDao.saveSupplierOrder(order);
		
		for (MaterialsBatchDetail detail : batchDetails) {
			int materialsId = detail.getMaterialsId();
			//插入库存变动明细表
			MaterialsChangeDetail changeDetail = new MaterialsChangeDetail();
			changeDetail.setMaterialsId(materialsId);
			changeDetail.setVoucherNo(order.getId());
			changeDetail.setChangeType(MaterialsChangeDetail.PROCUREMENT_UNQUALIFIED_OUTSTORE);
			changeDetail.setBatchNo(detail.getBatchNo());
			changeDetail.setNumber(detail.getNumber());
			materialsChangeDetailDao.save(changeDetail);
			
			//更新实时库存数量
			MaterialsSKUDetail skuDetail = materialsSKUDetailDao.getMaterialsSKUDetail(materialsId);
			skuDetail.setDefectiveNumber(skuDetail.getDefectiveNumber() - detail.getNumber());
			materialsSKUDetailDao.saveMaterialsSKUDetail(skuDetail);
			
			//更新批次库存数量
			MaterialsBatchDetail batchDetail = materialsBatchDetailDao.getBatchDetail(materialsId, detail.getBatchNo());
			//这里是合格的入库数量,使用了共用表，字段名有些歧义
			//plan_number 表示不合格入库数量
			//number 表示不合格出库数量
			batchDetail.setNumber(batchDetail.getNumber() + detail.getNumber());
			materialsBatchDetailDao.save(batchDetail);
		}
		
		//生成应付款记录
		StatementRecord record = new StatementRecord();
		record.setInOutType(StatementRecord.STATEMENT_IN_OUT_TYPE_OUT);
		record.setRecordType(StatementRecord.STATEMENT_RECORD_TYPE_DEC);
		record.setOrderId(order.getId());
		//record.setRecordDate(order.getCreateDate());
		record.setCustomerId(order.getSupplierId());
		record.setCustomerName(order.getSupplierName());
		record.setAmount(order.getTotleAmount());
		statementRecordDao.saveStatementRecord(record);

	}
}
